package cn.icanci.loopstack.bic.dal.mongo.mongo;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import cn.icanci.loopstack.bic.dal.mongo.common.PageList;
import cn.icanci.loopstack.bic.dal.mongo.daointerface.UnitViewSnapshotDAO;
import cn.icanci.loopstack.bic.dal.mongo.dateobject.UnitViewSnapshotDO;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/20 22:07
 */
@Service("unitViewSnapshotDAO")
public class MongoUnitViewSnapshotDAO extends AbstractBaseDAO<UnitViewSnapshotDO> implements UnitViewSnapshotDAO {
    @Override
    public void insert(UnitViewSnapshotDO unitViewSnapshotDO) {
        super.insert(unitViewSnapshotDO);
        mongoTemplate.insert(unitViewSnapshotDO, COLLECTION_NAME);
    }

    @Override
    public void update(UnitViewSnapshotDO unitViewSnapshotDO) {
        super.update(unitViewSnapshotDO);
        mongoTemplate.save(unitViewSnapshotDO, COLLECTION_NAME);
    }

    @Override
    public List<UnitViewSnapshotDO> queryAll() {
        Criteria criteria = Criteria.where(UnitViewSnapshotColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.find(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public PageList<UnitViewSnapshotDO> pageQuery(UnitViewSnapshotDO unitViewSnapshotDO, int pageNum, int pageSize) {
        Criteria criteria = Criteria.where(UnitViewSnapshotColumn.ENV).is(DEFAULT_ENV);
        if (StringUtils.isNotBlank(unitViewSnapshotDO.getSystemUuid())) {
            criteria.and(UnitViewSnapshotColumn.SYSTEM_UUID).is(unitViewSnapshotDO.getSystemUuid());
        }
        if (StringUtils.isNotBlank(unitViewSnapshotDO.getUseCaseUuid())) {
            criteria.and(UnitViewSnapshotColumn.USE_CASE_UUID).is(unitViewSnapshotDO.getUseCaseUuid());
        }
        Query query = new Query(criteria);
        query.with(Sort.by(Sort.Direction.DESC, UnitViewSnapshotColumn.CREATE_TIME));
        return pageQuery(query, COLLECTION_CLASS, pageSize, pageNum, COLLECTION_NAME);
    }

    @Override
    public UnitViewSnapshotDO queryOneById(String _id) {
        Criteria criteria = Criteria.where(UnitViewSnapshotColumn._ID).is(_id);
        criteria.and(UnitViewSnapshotColumn.ENV).is(DEFAULT_ENV);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public Class<? extends BaseColumn> getBaseColumn() {
        return UnitViewSnapshotColumn.class;
    }

    @Override
    public UnitViewSnapshotDO queryBySystemUuidAndUseCaseUuid(String systemUuid, String useCaseUuid) {
        Criteria criteria = Criteria.where(UnitViewSnapshotColumn.USE_CASE_UUID).is(useCaseUuid);
        criteria.and(UnitViewSnapshotColumn.ENV).is(DEFAULT_ENV);
        criteria.and(UnitViewSnapshotColumn.SYSTEM_UUID).is(systemUuid);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }

    @Override
    public UnitViewSnapshotDO queryBySystemUuidAndUseCaseUuidAndVersion(String systemUuid, String useCaseUuid, long version) {
        Criteria criteria = Criteria.where(UnitViewSnapshotColumn.USE_CASE_UUID).is(useCaseUuid);
        criteria.and(UnitViewSnapshotColumn.ENV).is(DEFAULT_ENV);
        criteria.and(UnitViewSnapshotColumn.SYSTEM_UUID).is(systemUuid);
        criteria.and(UnitViewSnapshotColumn.VERSION).is(version);
        Query query = new Query(criteria);
        return mongoTemplate.findOne(query, COLLECTION_CLASS, COLLECTION_NAME);
    }
}
